package com.hk.yajin.function.messages;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.hk.developmentlib.utills.DateUtils;
import com.hk.developmentlib.utills.StringHelper;
import com.hk.developmentlib.utills.UiUtil;
import com.hk.yajin.R;
import com.hk.yajin.base.MyApplication;
import com.hk.yajin.model.Message;

import java.util.List;

/**
 * https://github.com/CymChad/BaseRecyclerViewAdapterHelper
 */
public class MessageListQuickAdapter extends BaseQuickAdapter<Message, BaseViewHolder> {

    public MessageListQuickAdapter(List<Message> datas, Context ct) {
        super(R.layout.list_item_message, datas);
    }

    @Override
    protected void convert(final BaseViewHolder helper, Message item) {

        helper.setText(R.id.tv_time, StringHelper.friendlyPubTime(item.getCreate_time() * 1000L));

        if (helper.getAdapterPosition() == 0) {
            helper.setText(R.id.tv_day,DateUtils.getMessageDD(item.getCreate_time()));
            helper.setText(R.id.tv_ymd, DateUtils.getMessageYMD(item.getCreate_time()));
            helper.setVisible(R.id.lly_ymd, true);

        } else {
            Message lastMessage = getItem(helper.getAdapterPosition() - 1);
            if (DateUtils.getMessageYMD(item.getCreate_time()).equals(DateUtils.getMessageYMD(lastMessage.getCreate_time()))) {
                helper.setVisible(R.id.lly_ymd, false);
                helper.setText(R.id.tv_day,DateUtils.getMessageDD(item.getCreate_time()));
                helper.setText(R.id.tv_ymd, DateUtils.getMessageYMD(item.getCreate_time()));
            } else {
                helper.setText(R.id.tv_day,DateUtils.getMessageDD(item.getCreate_time()));
                helper.setText(R.id.tv_ymd, DateUtils.getMessageYMD(item.getCreate_time()));
                helper.setVisible(R.id.lly_ymd, true);
            }

        }

        if (item.getMail_attachs() != null && item.getMail_attachs().size() > 0) {
            helper.setVisible(R.id.iv_attach, true);
        } else {
            helper.setVisible(R.id.iv_attach, false);
        }

        if (helper.getAdapterPosition() == 0) {//是否第一条
            helper.getView(R.id.view_line1).setVisibility(View.INVISIBLE);
            helper.getView(R.id.view_line2).setVisibility(View.INVISIBLE);
            helper.getView(R.id.view_line3).setVisibility(View.GONE);
            helper.getView(R.id.view_line4).setVisibility(View.GONE);
        } else if (helper.getAdapterPosition() == getItemCount() - 2) {
            helper.getView(R.id.view_line1).setVisibility(View.VISIBLE);
            helper.getView(R.id.view_line2).setVisibility(View.VISIBLE);
            helper.getView(R.id.view_line3).setVisibility(View.VISIBLE);
            helper.getView(R.id.view_line4).setVisibility(View.VISIBLE);
        } else {
            helper.getView(R.id.view_line1).setVisibility(View.VISIBLE);
            helper.getView(R.id.view_line2).setVisibility(View.VISIBLE);
            helper.getView(R.id.view_line3).setVisibility(View.GONE);
            helper.getView(R.id.view_line4).setVisibility(View.GONE);
        }


        final TextView textView = helper.getView(R.id.tv_content);
        if (!TextUtils.isEmpty(item.getContent())) {  //内容
            textView.setText(item.getContent());
        } else {
            textView.setText("");
        }

        if (!TextUtils.isEmpty(item.getColor())) {
            textView.setTextColor(Color.parseColor(item.getColor()));
        } else {
            textView.setTextColor(UiUtil.getColor(R.color.black_222));
        }


//        final SpannableString msp = new SpannableString(textView.getText() + ".");
//        Drawable drawable = UiUtil.getDrawable(R.mipmap.test);
//        drawable.setBounds(0, 0, UiUtil.dip2px(14), UiUtil.dip2px(14));
//        msp.setSpan(new ImageSpan(drawable), textView.getText().length(), textView.getText().length()+1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
//        textView.setText(msp);
        textView.post(new Runnable() {
            @Override
            public void run() {
                final int line = textView.getLineCount();
                if (line > 3) {
                    textView.setMaxLines(3);
                    helper.addOnClickListener(R.id.tv_content);
                    textView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (textView.getLineCount() == 3) {
                                textView.setMaxLines(line);
                                //textView.setText(finalText);
                            } else {
                                textView.setMaxLines(3);
                                //textView.setText(msp);
                                //设置图片
                            }
                        }
                    });
                }
            }
        });
        helper.addOnClickListener(R.id.iv_attach);
        helper.addOnClickListener(R.id.ctv_zan);
    }


}
